package com.dzh.controller;

import com.dzh.mapper.CommentMapper;
import com.dzh.mapper.TourMapper;
import com.dzh.mapper.UserMapper;
import com.dzh.mapper.UserTourMapper;
import com.dzh.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class TourController {
    @Autowired
   private TourMapper tourMapper;
    @Autowired
    private UserTourMapper userTourMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/tourlist")
    public String tourlist(Model model){
     List<Tour> tourList =   tourMapper.queryAllTour();
     model.addAttribute("tourlist",tourList);


        return "tour_list";
    }




    @RequestMapping("/hottourlist")
    public String hottourlist(Model model){
        List<Tour> tourList = tourMapper.queryAllHotTour();
        model.addAttribute("tourlist",tourList);
        return "tour_list";
    }


    @RequestMapping("/tourdetail")
    public String touedetail(Model model,String tourid,HttpSession session){
      Tour tourdetail =  tourMapper.queryTourdetail(tourid);
      model.addAttribute("tourdetaiil",tourdetail);

        List<Comment> commentList = commentMapper.queryAllComment(tourid);

        for (Comment comment : commentList) {
            comment.setUserimg(userMapper.queryUserByusername1(comment.getUsername()).getUserimg());


        }


        model.addAttribute("commentlist",commentList);

        List<Tour> tourList = tourMapper.queryAllHotTour();
        model.addAttribute("tourlist",tourList);




        return  "tour_detail";
    }




    @RequestMapping("/buytour")
    public String buytour(Model model, HttpSession session, HttpServletResponse response,String tourid) throws IOException {
      String username = (String)session.getAttribute("loginUser");
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        PrintWriter out=response.getWriter();
        if(username==null){
            out.println("<script>alert('请先登录');</script>");
            return "login";
        }
        System.out.println(tourid);
        userTourMapper.addUserTour(username,tourid);
        out.println("<script>alert('购买成功');</script>");

      return "index";
    }



@RequestMapping("/querytourbyusername")
    public String querytourbyusername(Model model,HttpSession session,HttpServletResponse response) throws IOException {
    String username = (String)session.getAttribute("loginUser");
    response.setContentType("text/html;utf8");
    response.setCharacterEncoding("utf8");
    PrintWriter out=response.getWriter();
    if(username==null){
        out.println("<script>alert('请先登录');</script>");
        return "login";
    }
        List<UserTour> userTourList = userTourMapper.queryTourByUsername(username);
    for(UserTour userTour:userTourList){
        Tour tour = tourMapper.queryTourdetail(userTour.getTourid());

        userTour.setTour(tour);
    }
        model.addAttribute("tourlist",userTourList);
        return "usertourguanli";


}
    @RequestMapping("/deleteusertour")
    public String deleteusertour(Model model,HttpSession session,String tourid,HttpServletResponse response) throws IOException {
        String username = (String)session.getAttribute("loginUser");
     userTourMapper.deleteusertour(username,tourid);
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        PrintWriter out=response.getWriter();
        out.println("<script>alert('退订成功');</script>");
     return "index";
    }


    @RequestMapping("/addComment")
    public String addComment(Model model,String tourid,HttpSession session,String neiyong,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        PrintWriter out=response.getWriter();

        Comment comments = new Comment();
        String username = (String)session.getAttribute("loginUser");

        if(username==null){
            out.println("<script>alert('你还未登录，请先登录');</script>");
            return "login";
        }

        comments.setUsername(username);
        comments.setTourid(tourid);
        comments.setNeiyong(neiyong);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        comments.setCtime(df.format(new Date()));


        commentMapper.addComment(comments);


        Tour tourdetail =  tourMapper.queryTourdetail(tourid);
        model.addAttribute("tourdetaiil",tourdetail);


       List<Comment> commentList = commentMapper.queryAllComment(tourid);



        for (Comment comment : commentList) {
            comment.setUserimg(userMapper.queryUserByusername1(comment.getUsername()).getUserimg());


        }


       model.addAttribute("commentlist",commentList);

        List<Tour> tourList = tourMapper.queryAllHotTour();
        model.addAttribute("tourlist",tourList);

        return  "tour_detail";
    }





    @RequestMapping("/deletecomment")
    public String deletecomment(Model model,String username,String ctime,HttpSession session,HttpServletResponse response,String tourid) throws IOException {
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        PrintWriter out=response.getWriter();

     String   usernamenow  =(String)session.getAttribute("loginUser");
     System.out.println(username);
     System.out.println(usernamenow);
     if(usernamenow.equals("admin")){
         commentMapper.deleteComment(username,ctime);
         out.println("<script>alert('删除成功');</script>");


         List<Comment> commentList = commentMapper.queryAllComment(tourid);



         for (Comment comment : commentList) {
             comment.setUserimg(userMapper.queryUserByusername1(comment.getUsername()).getUserimg());


         }


         model.addAttribute("commentlist",commentList);


         Tour tourdetail =  tourMapper.queryTourdetail(tourid);
         model.addAttribute("tourdetaiil",tourdetail);


         List<Tour> tourList = tourMapper.queryAllHotTour();
         model.addAttribute("tourlist",tourList);

         return "tour_detail";
     }



        else if (!username.equals(usernamenow)) {
            out.println("<script>alert('这条不是你的评论无法删除');</script>");

            List<Comment> commentList = commentMapper.queryAllComment(tourid);



            for (Comment comment : commentList) {
                comment.setUserimg(userMapper.queryUserByusername1(comment.getUsername()).getUserimg());


            }


            model.addAttribute("commentlist",commentList);


            Tour tourdetail =  tourMapper.queryTourdetail(tourid);
            model.addAttribute("tourdetaiil",tourdetail);


            List<Tour> tourList = tourMapper.queryAllHotTour();
            model.addAttribute("tourlist",tourList);



            return "tour_detail";
        }




        commentMapper.deleteComment(username,ctime);
        out.println("<script>alert('删除成功');</script>");


        List<Comment> commentList = commentMapper.queryAllComment(tourid);



        for (Comment comment : commentList) {
            comment.setUserimg(userMapper.queryUserByusername1(comment.getUsername()).getUserimg());


        }


        model.addAttribute("commentlist",commentList);


        Tour tourdetail =  tourMapper.queryTourdetail(tourid);
        model.addAttribute("tourdetaiil",tourdetail);


        List<Tour> tourList = tourMapper.queryAllHotTour();
        model.addAttribute("tourlist",tourList);

        return "tour_detail";
    }



    @RequestMapping("/detailbyname")
    public String detailbyname(Model model,String tourname){
          List<Tour> tourList = tourMapper.queryTourdetailByname(tourname);
          model.addAttribute("tourlist",tourList);
          return "tour_list";
    }



   @RequestMapping("/queryTourCity")
    public String queryTourCity(Model model,String tourplace){
      List<Tour> tourList =   tourMapper.queryTourByCity(tourplace);
       System.out.println(tourplace);
       model.addAttribute("tourlist",tourList);
       return "tour_list";
   }



}
